package com.damocode.starter.httpclient.autoconfigure;

import com.damocode.starter.httpclient.exceptions.ClientException;
import com.damocode.starter.httpclient.helper.HttpClientHelper;
import org.apache.http.client.HttpClient;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.Resource;

@Configuration
@ConditionalOnClass(HttpClient.class)
@EnableConfigurationProperties(HttpClientPoolProperties.class)
public class HttpClientAutoConfiguration {

    @Resource
    private HttpClientPoolProperties httpClientPoolProperties;

    @Bean
    @ConditionalOnMissingBean(HttpClientHelper.class)
    public HttpClientHelper httpClientHelper() throws ClientException {
        return HttpClientHelper.getInstance(httpClientPoolProperties);
    }

}
